﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using WaterBill.BUS;
using WaterBill.DTO;

namespace WaterBill.GUI
{
    public partial class frmCharge : DevExpress.XtraEditors.XtraForm
    {
        public frmCharge()
        {
            InitializeComponent();
        }

        BUS_Charges buscharge = new BUS_Charges();
        BUS_Bills busbill = new BUS_Bills();
        BUS_Customers buscus = new BUS_Customers();
        
        //load du lieu len combobox trang thai
        public void loadcbbStatus() 
        {
            DataTable dtStatus = new DataTable();
            dtStatus.Columns.Add("Value", typeof(int));
            dtStatus.Columns.Add("Text");
            DataRow drStatus;
            drStatus = dtStatus.NewRow();
            drStatus["Value"] = 1;
            drStatus["Text"] = "Hoạt động";
            dtStatus.Rows.Add(drStatus);
            drStatus = dtStatus.NewRow();
            drStatus["Value"] = 2;
            drStatus["Text"] = "Không hoạt động";
            dtStatus.Rows.Add(drStatus);
            cbbStatus.DataSource = dtStatus;
            cbbStatus.DisplayMember = "Text";
            cbbStatus.ValueMember = "Value";
            cbbStatus.SelectedIndex = 0;
        }

        private void frmCharge_Load(object sender, EventArgs e)
        {
            loadDataForm();
        }

        private void loadDataForm()
        {
            gcCharge.DataSource = buscharge.BUS_Charges_GetAll();
            loadcbbStatus();
            DataTable dtChargeMax = buscharge.BUS_Charge_GetByMaxId();
            if (dtChargeMax.Rows.Count > 0)
            {
                string date = DateTime.Today.ToString("M/yyyy");//lay thang hien tai
                string maxdate = dtChargeMax.Rows[0]["NameCharge"].ToString();//lay thang lon nhat trong csdl               
                if (date == maxdate)
                {
                    btnAdd.Enabled = false;
                }
            }
            else
            {
                btnAdd.Enabled = true;
            }
        }

        private void gvCharge_RowClick(object sender, RowClickEventArgs e)
        {
            btnEdit.Enabled = true;
            btnSave.Enabled = false;
            btnCancel.Enabled = false;
            try
            {
                txtChargeid.Text = Convert.ToString(gvCharge.GetRowCellValue(e.RowHandle, "ChargeId"));
                txtPrice.Text = Convert.ToString(gvCharge.GetRowCellValue(e.RowHandle, "Price"));
                cbbMonth.Text = Convert.ToString(gvCharge.GetRowCellValue(e.RowHandle, "Month"));
                cbbYear.Text = Convert.ToString(gvCharge.GetRowCellValue(e.RowHandle, "Year"));
                cbbStatus.SelectedValue = Convert.ToString(gvCharge.GetRowCellValue(e.RowHandle, "Status"));
            }
            catch
            {
                btnEdit.Enabled = false;
            }
        }
        private void btnPrint_Click(object sender, EventArgs e)
        {
            gcCharge.ShowPrintPreview();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (busbill.BUS_Bills_GetNewIndexNull().Rows.Count > 0)
            {

                //XtraMessageBox.Show(max_chrgeid.ToString(), "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                XtraMessageBox.Show("Lỗi. Bạn chưa thêm đầy đủ chỉ số mới cho tháng hiện tại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                frmAddBill frm = new frmAddBill();
                frm.ShowDialog();
            }
            else
            {
                btnAdd.Enabled = false;
                btnEdit.Enabled = false;
                btnCancel.Enabled = true;
                btnSave.Enabled = true;

                string month = DateTime.Today.Month.ToString();
                string year = DateTime.Today.Year.ToString();

                //MessageBox.Show(month + "/" + year);
                cbbMonth.Text = month;
                cbbMonth.ValueMember = month;
                cbbYear.Text = year;
                cbbMonth.ValueMember = year;

                txtPrice.Enabled = true;
                cbbStatus.Enabled = true;
                txtPrice.Text = "";
                txtChargeid.Text = "";
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            btnAdd.Enabled = true;
            btnSave.Enabled = false;
            btnCancel.Enabled = false;
            txtPrice.Enabled = false;
            cbbStatus.Enabled = false;
            if (txtChargeid.Text == "")
            {
                txtPrice.Text = "";
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            string date = DateTime.Today.ToString("M/yyyy");//lay thang hien tai
            string maxdate = buscharge.BUS_Charge_GetByMaxId().Rows[0]["NameCharge"].ToString();//lay thang lon nhat trong csdl    

            if (date != maxdate)
            {
                btnAdd.Enabled = true;
            }
            else btnAdd.Enabled = false;
            btnSave.Enabled = true;
            btnCancel.Enabled = true;
            txtPrice.Enabled = true;
            cbbStatus.Enabled = true;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (txtPrice.Text != "")
            {
                splashScreenManager1.ShowWaitForm();

                DTO_Charges obj = new DTO_Charges();
                obj.Price = Convert.ToDecimal(txtPrice.Text);
                obj.Month = Convert.ToInt32(cbbMonth.Text);
                obj.Year = Convert.ToInt32(cbbYear.Text);
                obj.Status = Convert.ToInt32(cbbStatus.SelectedValue);
                if (txtChargeid.Text == "")
                {//Thực hiện thêm
                    if (buscharge.BUS_Charges_Insert(obj))
                    {
                        int row_num = buscus.BUS_Customers_GetStatusEnable().Rows.Count;
                        DTO_Bills objBill = new DTO_Bills();
                        objBill.ChargeId = Convert.ToInt32(buscharge.BUS_Charge_GetByMaxId().Rows[0]["ChargeId"].ToString());
                        try
                        {
                            for (int i = 0; i < row_num; i++)
                            {
                                objBill.CustomerId = Convert.ToInt32(buscus.BUS_Customers_GetStatusEnable().Rows[i]["CustomerId"].ToString());
                                busbill.BUS_Bills_Insert(objBill);
                            }
                            XtraMessageBox.Show("Thêm thành công.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                        }
                        catch
                        {
                            XtraMessageBox.Show("Lỗi, không thể thêm được.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                    else {
                        XtraMessageBox.Show("Lỗi, không thể thêm được.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else { 
                    //Thực hiện sửa
                    obj.ChargeId = Convert.ToInt32(txtChargeid.Text);
                    //MessageBox.Show(txtPrice.Text + "," + cbbMonth.Text + "," + cbbYear.Text + "," + cbbStatus.SelectedValue);
                    if (buscharge.BUS_Charges_Update(obj))
                    {
                        XtraMessageBox.Show("Sửa thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                        btnEdit.Enabled = true;
                    }
                    else
                    {
                        MessageBox.Show("Lỗi, Không thể sửa được.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }

                btnAdd.Enabled = true;
                btnSave.Enabled = false;
                btnCancel.Enabled = false;
                txtPrice.Enabled = false;
                cbbStatus.Enabled = false;
 
                loadDataForm();
                splashScreenManager1.CloseWaitForm();
            }
            else {
                MessageBox.Show("Giá không được bỏ trống", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtPrice.Focus();
                txtPrice.BackColor = Color.Cyan;
            }
        }

        

        
    }
}